package com.vhall.component.service.goods.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.vhall.component.dao.goods.LiveGoodsMapper;
import com.vhall.component.dao.goods.LiveGoodsStatsDetailMapper;
import com.vhall.component.dao.goods.LiveGoodsStatsMapper;
import com.vhall.component.dao.room.RoomGoodsMapper;
import com.vhall.component.entity.goods.LiveGoodsEntity;
import com.vhall.component.entity.goods.LiveGoodsStatsDetailEntity;
import com.vhall.component.entity.goods.LiveGoodsStatsEntity;
import com.vhall.component.entity.goods.goods.GoodsClickGoodsReqVO;
import com.vhall.component.entity.goods.goods.GoodsPushScreenReqVO;
import com.vhall.component.entity.goods.vo.GetGoodsInfoRequestVO;
import com.vhall.component.entity.goods.vo.GetGoodsInfoResponseVO;
import com.vhall.component.entity.room.dto.RoomsEntityDTO;
import com.vhall.component.entity.room.entity.RoomGoodsEntity;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.plugin.common.support.Token;
import com.vhall.component.plugin.common.support.TokenThreadLocal;
import com.vhall.component.plugin.core.service.PaasService;
import com.vhall.component.plugin.core.service.event.PaasMessageReqDTO;
import com.vhall.component.service.goods.GoodsService;
import com.vhall.component.service.goods.GuoxinPrdService;
import com.vhall.component.service.goods.LiveGoodsStatsService;
import com.vhall.component.service.goods.common.GoodsConstant;
import com.vhall.component.service.room.RoomInnerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author guoxin.wang
 * @date 2021-06-10 15:21
 */
@Slf4j
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private LiveGoodsStatsDetailMapper liveGoodsStatsDetailMapper;

    @Autowired
    private LiveGoodsMapper liveGoodsMapper;

    @Autowired
    private RoomInnerService roomInnerService;
    @Autowired
    private RoomGoodsMapper roomGoodsMapper;
    @Resource
    private GuoxinPrdService guoxinPrdService;
    @Autowired
    private PaasService paasService;
    @Autowired
    private LiveGoodsStatsMapper liveGoodsStatsMapper;


    @Override
    public Boolean clickGoods(GoodsClickGoodsReqVO reqVO) {
        // 1、根据token获取用户信息。
        Token accountInfo = TokenThreadLocal.getToken();
        // 2、获取房间信息。
        RoomsEntityDTO roomInfo = roomInnerService.getRoomInfo(reqVO.getRoomId());
        if (roomInfo == null) {
            throw new BusinessException(BizErrorCode.EMPTY_ROOM);
        }
        LiveGoodsStatsDetailEntity param = new LiveGoodsStatsDetailEntity();
        param.setBgId(reqVO.getBgId());
        param.setIlId(roomInfo.getIlId());
        param.setType(GoodsConstant.OPERATE_TYPE_CLICK);
        param.setUserId(accountInfo.getAccountId());
        return liveGoodsStatsDetailMapper.insert(param) > 0;
    }

    @Override
    public List<LiveGoodsEntity> listLiveGoods(int bgId) {
        LambdaQueryWrapper<LiveGoodsEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(LiveGoodsEntity::getBgId, bgId).eq(LiveGoodsEntity::getGoodsStatus, GoodsConstant.GOODS_STATUS_ON);
        return liveGoodsMapper.selectList(wrapper);
    }

    @Override
    public Boolean pushScreen(GoodsPushScreenReqVO reqVO) {
        RoomGoodsEntity roomGoodsEntity = roomGoodsMapper.selectById(reqVO.getId());
        if (roomGoodsEntity == null) {
            throw new BusinessException(BizErrorCode.EMPTY_DATA);
        }
        RoomsEntityDTO roomsEntityDTO = roomInnerService.getRoomInfoById(reqVO.getIlId());

        GetGoodsInfoRequestVO requestVO = new GetGoodsInfoRequestVO();
        requestVO.setPrdCodes(roomGoodsEntity.getPrdType() + "." + roomGoodsEntity.getPrdCode());
        requestVO.setIlId(roomGoodsEntity.getIlId());
        List<GetGoodsInfoResponseVO> goodsInfoList = guoxinPrdService.getGoodsInfoList(requestVO);
        if (CollUtil.isEmpty(goodsInfoList)){
            throw new BusinessException(60000,"未查询到商品信息","");
        }
        GetGoodsInfoResponseVO getGoodsInfoResponseVO = goodsInfoList.get(0);
        if (reqVO.getSerialNumber() != null){
            getGoodsInfoResponseVO.setSerialNumber(reqVO.getSerialNumber());
        }
        JSONObject messageBody = new JSONObject();
        String s = JSONObject.toJSONString(getGoodsInfoResponseVO);
        messageBody = (JSONObject) JSON.parse(s);
        messageBody.put("type", GoodsConstant.RECORD_TYPE_3001);
        messageBody.put("screen_second", reqVO.getScreenSecond());

        PaasMessageReqDTO paasMessageReq = new PaasMessageReqDTO.Builder(roomsEntityDTO.getChannelId(), messageBody.toString())
                .initDefault().build();
        paasMessageReq.setType("service_custom");
        paasService.sendMessage(paasMessageReq);


        LambdaQueryWrapper<LiveGoodsStatsEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(LiveGoodsStatsEntity::getIlId, roomGoodsEntity.getIlId());
        queryWrapper.eq(LiveGoodsStatsEntity::getBgId, roomGoodsEntity.getId());
        queryWrapper.last(" limit 1");
        LiveGoodsStatsEntity goodsStatsEntity = liveGoodsStatsMapper.selectOne(queryWrapper);
        if (goodsStatsEntity == null){
            goodsStatsEntity = new LiveGoodsStatsEntity();
            goodsStatsEntity.setBgId(roomGoodsEntity.getId());
            goodsStatsEntity.setIlId(roomGoodsEntity.getIlId());
            goodsStatsEntity.setPushScreenNum(1);
            goodsStatsEntity.setDuration(reqVO.getScreenSecond());
            goodsStatsEntity.setCreateDate(DateUtil.format(new Date(), "yyyy-MM-dd"));
            liveGoodsStatsMapper.insert(goodsStatsEntity);
        }else {
            goodsStatsEntity.setDuration(goodsStatsEntity.getDuration() + reqVO.getScreenSecond());
            goodsStatsEntity.setPushScreenNum(goodsStatsEntity.getPushScreenNum() + 1);
            liveGoodsStatsMapper.updateById(goodsStatsEntity);
        }
        return true;
    }

}

